/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session;

import entities.Film;
import entities.Stock;
import java.io.File;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import wrapper.WrapperXmlFilm;

/**
 *
 * @author Indowax
 */
@Stateless
@LocalBean
public class GestionnaireStock {
    
    @PersistenceContext(unitName = "ProjetWebPU")
    private EntityManager em;
    private WrapperXmlFilm xmlFilm;

    
      public void persist(Object object) {
        em.persist(object);
    }

       public void creerStocksDeTest() {
        xmlFilm = new WrapperXmlFilm();
        String[] listeFichierXml;
        int i;
       // File file = new File("C:\\Users\\Indowax\\Documents\\NetBeansProjects\\ProjetWeb\\web\\xml\\stock");
       File file = new File("C:\\Users\\asus\\Documents\\M1\\AppliWeb\\projet\\trunk\\ProjetWeb\\web\\xml\\stock");
        listeFichierXml = file.list();
        for (i = 0; i < listeFichierXml.length; i++) { 
            File newFile = new File(file + "\\" + listeFichierXml[i]);
            try {
                if (xmlFilm.readXml(newFile))
                {
                   // Stock s = new Stock(xmlFilm.getQuantiteDVD(), xmlFilm.getIdFilm());
                   
                  //  persist(s);
                }
            } catch (Exception e) {
                //Erreur
            }
        }
    }
  
    public int getStockFilm(int id) {
        Query q = getEm().createQuery("select quantite from Stock as quantite join quantite.Film f where f.id='" + id + "'");
        return (int) Integer.parseInt((String) q.getSingleResult());
    }

    /**
     * @return the em
     */
    public EntityManager getEm() {
        return em;
    }

    /**
     * @param em the em to set
     */
    public void setEm(EntityManager em) {
        this.em = em;
    }
    
    
}
